package com.gitee.wsl.struct.grammar.ast.node.sample


import com.gitee.wsl.struct.grammar.ast.node.ASTNode
import com.gitee.wsl.struct.grammar.ast.node.Node
import com.gitee.wsl.struct.grammar.ast.node.NodeType
import com.gitee.wsl.struct.grammar.parser.ParsingScope
import com.gitee.wsl.struct.grammar.parser.token.Token
import com.gitee.wsl.struct.grammar.parser.token.TokenMatch

/**
 * A leaf-element of the [ASTNode].
 */
data class Lexeme(
    val match: TokenMatch,
    val text: String
) : Node(type = NodeType.LEXEME, startOffset = match.offset, endOffset = match.offset + match.length)

/**
 * Parses [token] and returns corresponding lexeme.
 */
suspend fun ParsingScope.lexeme(token: Token): Lexeme {
    val match = token()
    return Lexeme(match, match.text)
}

val NodeType.Companion.LEXEME get() = NodeType("LEXEME")
